﻿using Developer.Core.FrameWork.ClusterLoadBalance.Impl;
using Developer.Core.FrameWork.ClusterLoadBalance.Options;
using Developer.Core.FrameWork.ClusterLoadBalance.Service;
using Developer.Core.FrameWork.GlobalVar;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Developer.Core.FrameWork.ClusterLoadBalance.Extensions
{
    /// <summary>
    /// 负载均衡扩展类
    /// </summary>
    public static class LoadBalanceCollectionExtensions
    {
        /// <summary>
        /// 注册负载均衡
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddLoadBalanceService(this IServiceCollection services)
        {
            AddLoadBalanceService(services, option => { });

            return services;
        }

        public static IServiceCollection AddLoadBalanceService(this IServiceCollection services,Action<LoadBalanceOptions> option)
        {
            // 是否开启负载均衡
            if (!GlobalVars.LoadBalance)
                return services;

            services.Configure<LoadBalanceOptions>(option);

            services.AddSingleton<ILoadBalance, RandomLoadBalance>();

            return services;
        }
    }
}
